package com.he.controller;

import com.he.entity.City;
import com.he.entity.Hotel;
import com.he.entity.ResultData;
import com.he.entity.Room;
import com.he.feign.CityFeign;
import com.he.feign.HotelFeign;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system")
@Slf4j
public class SystemController {

    @Autowired
    private CityFeign cityFeign;
    @Autowired
    private HotelFeign hotelFeign;


    //文件上传路径
    private static final String UPLOAD_PATH =
            "D:\\aaa\\sz1913\\reserve_platform\\micro_service\\micro_system\\images";


    /**
     * 跳转到客房管理页面
     * @return
     */
    @RequestMapping("/toHotelRoom/{hid}")
    public String toHotelRoom(@PathVariable("hid")Integer hid,Model model){
        //log.info("需要管理的酒店id:"+hid);
        ResultData<List<Room>> list = hotelFeign.getRoomByHid(hid);
        //log.info("查出的酒店:"+list);
        Hotel hotel = hotelFeign.getHotelByHid(hid);
        log.info("当前酒店："+hotel);
        model.addAttribute("hotel",hotel);
        model.addAttribute("rList",list);
        log.info("房间列表"+list);
        return "hotelroom";
    }

    /**
     * 跳转到城市添加页面
     * @return
     */
    @RequestMapping("/toCityAdd")
    public String toCityAdd(){
        return "cityadd";
    }

    /**
     * 跳转到酒店添加页面
     * @return
     */
    @RequestMapping("/toHotelAdd")
    public String toHotelAdd(Model model) {
        ResultData<List<City>> list = cityFeign.cityList();
        //log.info("查询出城市列表"+list);
        model.addAttribute("cList",list);
        return "hoteladd";
    }

    /**
     * 跳转到城市列表界面
     * @return
     */
    @RequestMapping("/toCityList")
    public String toCityList(Model model) {
        ResultData<List<City>> list = cityFeign.cityList();
        //log.info("查询出城市列表"+list);
        model.addAttribute("cList",list);
        return "citylist";
    }

    /**
     * 跳转到酒店列表界面
     * @return
     */
    @RequestMapping("/toHotelList")
    public String toHotelList(Model model){
        ResultData<List<Hotel>> hList = hotelFeign.hotelList();
        //log.info("展示酒店列表"+hList);
        model.addAttribute("hList",hList.getData());
        return "hotellist";
    }

    /**
     * 修改房间数量
     * @param bid
     * @param num
     * @return
     */
    @RequestMapping("/updateBNum/{bid}")
    public String updateBNum(@PathVariable("bid") Integer bid,Integer num){
        System.out.println("bid:"+bid+",num:"+num);
        hotelFeign.updateBNum(bid, num);
        return "redirect:system/toHotelRoom/"+bid;
    }

    /**
     * 根据图片路径，获取本地的图片
     * @param path
     * @return
     */
    @RequestMapping("/getImage")
    public void getImage(String path, HttpServletResponse response){
        try (
                InputStream in = new FileInputStream(path);
                OutputStream out = response.getOutputStream();
        ){
            IOUtils.copy(in, out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加城市
     * @return
     */
    @RequestMapping("/cityadd")
    public String cityAdd(City city){
        //调用城市服务，添加城市数据
        ResultData<Boolean> flag = cityFeign.citySave(city);
        //log.info("调用服务返回结果：" + flag);
        if (flag.getCode() == ResultData.Code.OK){
            return "succ";
        }
        return "cityerror";
    }

    /**
     * 添加房间类型
     * @param room
     * @param roomImage
     * @return
     */
    @RequestMapping("/roomadd")
    public String roomAdd(Room room,MultipartFile roomImage){
        System.out.println("添加房间类型:"+room);
        File updateFile = getFile(roomImage);
        //设置图片的上传的绝对路径
        room.setImage((updateFile.getAbsolutePath().replace("\\", "/")));
        //保存酒店到数据库
        room.setHid(room.getHid());
        room.setNumber(0);
        ResultData<Boolean> flag = hotelFeign.saveRoom(room);

        log.info("调用服务返回结果"+flag);
        if (flag.getCode() == ResultData.Code.OK){
            return "redirect:/system/toHotelRoom/"+room.getHid();
        }
        return "systemerror";
    }

    /**
     * 添加酒店
     * @param hotel
     * @return
     */
    @RequestMapping("/hoteladd")
    public String hotelAdd(Hotel hotel, MultipartFile image){
        File updateFile = getFile(image);
        //设置图片的上传的绝对路径
        hotel.setHotelImage(updateFile.getAbsolutePath().replace("\\", "/"));
        //保存房间类型到数据库
        ResultData<Boolean> flag = hotelFeign.saveHotel(hotel);
        //log.info("调用服务返回结果"+flag);
        if (flag.getCode() == ResultData.Code.OK){
            return "redirect:/system/toHotelList";
        }
        return "systemerror";
    }

    /**
     * 长传图片的方法
     * @param image
     * @return
     */
    private File getFile(MultipartFile image) {
        //处理上传文件名
        String fileName = UUID.randomUUID().toString()+".jpg";
        File file = new File(UPLOAD_PATH);
        if(!file.exists()){
            file.mkdirs();
        }
        File updateFile = new File(file, fileName);
        //处理文件上传
        try (
                InputStream in = image.getInputStream();
                OutputStream out = new FileOutputStream(updateFile);
                ){
            //上传
            IOUtils.copy(in,out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return updateFile;
    }

}
